package com.halixun.d2app.dialog;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;

import android.text.InputFilter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.halixun.d2app.R;
import com.halixun.d2app.utils.CashierInputFilter;
import com.halixun.d2app.utils.ToastUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class RefundDialogFragment extends DialogFragment implements View.OnClickListener {

    private static final String KEY_PARAMS = RefundDialogFragment.class.getSimpleName() + "key_params";

    private static final String KEY_ORDER_ID = RefundDialogFragment.class.getSimpleName() + "key_order_id";

    private EditText money, password;

    private double price;

    private String orderId;

    private RefundDialogListener mListener;

    public static RefundDialogFragment getInstance(double price, String orderId) {
        RefundDialogFragment f = new RefundDialogFragment();
        Bundle params = new Bundle();
        params.putDouble(KEY_PARAMS, price);
        params.putString(KEY_ORDER_ID, orderId);
        f.setArguments(params);
        return f;
    }


    public RefundDialogFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        price = getArguments().getDouble(KEY_PARAMS);
        orderId = getArguments().getString(KEY_ORDER_ID);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_refund_dialog, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        setListeners(view);
        initData();
    }

    private void initData() {
        money.setText(price + "");
        money.setSelection((price + "").length());
    }

    private void setListeners(View view) {
        view.findViewById(R.id.cancel).setOnClickListener(this);
        view.findViewById(R.id.confirm).setOnClickListener(this);
    }

    private void initView(View view) {
        money = view.findViewById(R.id.money);
        InputFilter[] filters = {new CashierInputFilter()};
        money.setFilters(filters);
        password = view.findViewById(R.id.password);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm:
                String moneyNum = money.getText().toString().trim();
                String passwordNum = password.getText().toString().trim();
                if (TextUtils.isEmpty(moneyNum)) {
                    ToastUtils.showCenterToast("请输入金额");
                    return;
                }
                if (TextUtils.isEmpty(passwordNum)) {
                    ToastUtils.showCenterToast("请输入密码");
                    return;
                }
                if (null != mListener) {
                    mListener.onRefundDialogListener(moneyNum, orderId, passwordNum);
                }
            case R.id.cancel:
                dismiss();
                break;
        }
    }

    public RefundDialogFragment setOnRefundDialogListener(RefundDialogListener listener) {
        this.mListener = listener;
        return this;
    }


    public interface RefundDialogListener {
        void onRefundDialogListener(String momey, String orderId, String password);
    }
}
